package com.wanxi.servlet;

import com.alibaba.fastjson2.JSONObject;
import com.wanxi.model.UserModel;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/aa")
public class IndexServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        1.乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());
//        2.得到前端数据
        String username = req.getParameter("username");
        String pass = req.getParameter("pass");
//        3.封装到model
        UserModel userModel = new UserModel();
        userModel.setName(username);
        userModel.setPass(pass);
//        4.逻辑处理
//        5.得到返回结果（也是第四阶段）
//        String s= JSONObject.toJSONString(userModel);
//        对req设置一个新属性，然后将值放入新属性里面
//        req.setAttribute("name", username);
//        req.setAttribute("pass", pass);
//        req.getAttribute("name");
//        req.removeAttribute("name");
        req.setAttribute("userModel", userModel);
//        req.getSession().setAttribute("name", username);
//        req.getSession().setAttribute("pass", pass);
//        req.getServletContext().setAttribute("name", username);
//        req.getServletContext().setAttribute("pass", pass);
//        上面这三种与都可以理解为key，value对象
//        6.返回给前端：页面转发
        req.getRequestDispatcher("/success.jsp").forward(req, resp);
//        如果只是单纯的跳转页面，则不要用页面转发
//        resp.sendRedirect("/success.jsp");

    }
}
